home *** CD-ROM | disk | FTP | other *** search
/ Speccy ClassiX 1998 / Speccy ClassiX 98.iso / amiga_system / the_aminet / dev / misc / calculus37_3.lha / calculus / Source / Ival.c next >
C/C++ Source or Header  |  1995-09-15  |  3KB  |  100 lines

  1. /**** main.c *******************************************************/
  2.  
  3. #include <exec/execbase.h>
  4. #include <dos/dos.h>
  5. #include <clib/dos_protos.h>
  6. #include <clib/exec_protos.h>
  7.  
  8. #include "calculus.h"
  9.  
  10. #define PROGNAME             "Ival"
  11. #define BUFSIZE              300
  12. #define NumArgs              4
  13.  
  14. char    ArgStr   [       ] = "VAR/K,TO/K,LF=LFORMAT/K,EXPRESSION/F";
  15. LONG    ArgArray [NumArgs] = { 0,0,0,0 };
  16.  
  17. const char version[      ] = "$VER: " PROGNAME "1.3 ⌐ David G÷hler (15-09-95)";
  18. const char *errstr[] = { 0L,"Stack empty",
  19.                             "Syntax error",
  20.                             "Stack overflow",
  21.                             "Variable not found",
  22.                             "Too much symbols",
  23.                             "Too much lexems",
  24.                             "Variable name too long",
  25.                             "Not enough memory",
  26.                             "Division by 0!",
  27.                             "Caller is not a process"
  28.                        };
  29. extern struct ExecBase *SysBase;
  30. struct Library         *CalculusBase;
  31. char                    outbuf [BUFSIZE];
  32. char                   *input;
  33.  
  34. int main(void)
  35. {
  36.    int retcode = RETURN_OK;
  37.    int result;
  38.    long error = RESULT_OK;
  39.    struct RDArgs *rda = NULL;
  40.  
  41.    if (SysBase->LibNode.lib_Version < 37) return 20;
  42.  
  43.    if (CalculusBase = OpenLibrary("calculus.library",0l))
  44.    {
  45.       memset(ArgArray,0,sizeof(int)*NumArgs);
  46.  
  47.       if (rda = ReadArgs(ArgStr,ArgArray,rda))
  48.       {
  49.          result = CalcInteger((char *)ArgArray[3],&error);
  50.          if (error == RESULT_OK)
  51.          {
  52.             if (ArgArray[2] != 0)      // LFORMAT
  53.             {  SPrintf(outbuf,(char *)(ArgArray[2]),result,result,result,result,result);
  54.             }
  55.             else
  56.             {  SPrintf(outbuf,"%ld",result); }
  57.  
  58.             if (ArgArray[0] != 0)      // VAR
  59.             {  SetVar((char *)(ArgArray[0]),outbuf,-1,LV_VAR); }
  60.             else if (ArgArray[1] != 0) // TO
  61.             {  BPTR filep;
  62.                if (filep = Open((char *)(ArgArray[1]),MODE_NEWFILE))
  63.                {
  64.                   if (Write(filep,outbuf,strlen(outbuf)) <= 0)
  65.                   {  PrintFault(IoErr(),"Ival");
  66.                      retcode = RETURN_FAIL;
  67.                   }
  68.                   Close(filep);
  69.                }
  70.                else
  71.                {  PrintFault(IoErr(),"Ival");
  72.                   retcode = RETURN_FAIL;
  73.                }
  74.             }
  75.             else
  76.             {  Printf("%s\n",outbuf); }
  77.          }
  78.          else
  79.          {  
  80.             Printf("Error: %s\n",errstr[error]);
  81.          }
  82.          FreeArgs(rda);
  83.       }
  84.       else
  85.       {  PrintFault(IoErr(),"Eval");
  86.          retcode = RETURN_FAIL;
  87.       }
  88.       CloseLibrary(CalculusBase);
  89.    }
  90.    else
  91.    {  Printf("Can't open %s version 0\n",CALCBASENAME);
  92.       retcode = RETURN_FAIL;
  93.    }
  94.  
  95.    if (error != RESULT_OK) retcode = RETURN_FAIL;
  96.  
  97.    return retcode;
  98. }
  99.  
  100.